﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Vimeo
{
    public class VimeoProvider : AbstractProvider
    {
        private VimeoConfigControl _configControl;

        public VimeoProvider()
        { }

        public VimeoProvider(string config)
            : base(config)
        {
            Service = new VimeoService(Config);
        }

        public override string Alias
        {
            get { return "Vimeo"; }
        }

        public VimeoService Service { get; set; }

        public override Interfaces.IConfigControl ConfigControl
        {
            get 
            {
                if (_configControl == null)
                    _configControl = new VimeoConfigControl(Config);

                return _configControl;
            }
        }

        public override Interfaces.ICreateControl CreateControl
        {
            get { throw new NotImplementedException(); }
        }

        public override MediaItem GetMediaItemById(string id)
        {
            return Service.GetVideo(Convert.ToInt32(id)).ToMediaItem();
        }

        public override IList<MediaItem> GetMediaItems(string parentId)
        {
            var mediaItems = new List<MediaItem>();

            if (parentId == "-1")
            {
                // Create root folders
                mediaItems.Add(new MediaItem
                {
                    Id = "albm:-1",
                    Title = "Albums",
                    Icon = UmbracoIcons.FolderOpen,
                    HasChildren = true,
                    PreviewImageUrl = "",
                    Selectable = false
                });

                mediaItems.Add(new MediaItem
                {
                    Id = "chnl:-1",
                    Title = "Channels",
                    Icon = UmbracoIcons.FolderOpen,
                    HasChildren = true,
                    PreviewImageUrl = "",
                    Selectable = false
                });

                mediaItems.Add(new MediaItem
                {
                    Id = "grp:-1",
                    Title = "Groups",
                    Icon = UmbracoIcons.FolderOpen,
                    HasChildren = true,
                    PreviewImageUrl = "",
                    Selectable = false
                });

                mediaItems.Add(new MediaItem
                {
                    Id = "all:-1",
                    Title = "All",
                    Icon = UmbracoIcons.FolderOpen,
                    HasChildren = true,
                    PreviewImageUrl = "",
                    Selectable = false
                });
            }
            else
            {
                var key = parentId.Substring(0, parentId.IndexOf(":"));
                var id = Convert.ToInt32(parentId.Substring(parentId.IndexOf(":") + 1));

                switch (key)
                {
                    case "albm":
                        if (id == -1)
                        {
                            // Get Albums
                            foreach (var mi in Service.GetAlbums().Select(a => a.ToMediaItem()))
                            {
                                mi.Id = "albm:" + mi.Id;
                                mediaItems.Add(mi);
                            }
                        }
                        else
                        {
                            // Get Album Videos
                            mediaItems.AddRange(Service.GetAlbumVideos(id).Select(v => v.ToMediaItem()).ToList());
                        }
                        break;
                    case "chnl":
                        if (id == -1)
                        {
                            // Get Channels
                            foreach (var mi in Service.GetChannels().Select(a => a.ToMediaItem()))
                            {
                                mi.Id = "chnl:" + mi.Id;
                                mediaItems.Add(mi);
                            }
                        }
                        else
                        {
                            // Get Channel Videos
                            mediaItems.AddRange(Service.GetChannelVideos(id).Select(v => v.ToMediaItem()).ToList());
                        }
                        break;
                    case "grp":
                        if (id == -1)
                        {
                            // Get Groups
                            foreach (var mi in Service.GetGroups().Select(a => a.ToMediaItem()))
                            {
                                mi.Id = "grp:" + mi.Id;
                                mediaItems.Add(mi);
                            }
                        }
                        else
                        {
                            // Get Group Videos
                            mediaItems.AddRange(Service.GetGroupVideos(id).Select(v => v.ToMediaItem()).ToList());
                        }
                        break;
                    default:
                        // Get All Videos
                        mediaItems.AddRange(Service.GetVideos().Select(v => v.ToMediaItem()).ToList());
                        break;
                }
            }

            return mediaItems;
        }
    }
}